﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.sysDAL;
using dhDataAcess.Model;
using dhDataAcess.dataAcess.sysDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.baseBLL;

namespace dhSystemBLL.basicData
{
   public  class userListBLL
    {
        private  userListTableAdapter  _userListAdapter = null;
        private userListTableAdapter  userListAdapter
        {
            get
            {
                if (_userListAdapter == null) _userListAdapter = new userListTableAdapter ();
                return _userListAdapter;
            }
        }
        /// <summary>
        /// 查询用户表格
        /// </summary>
        /// <returns></returns>

        public sysDataSet.userListDataTable getuserListAll()
        {
            try
            {
                return userListAdapter.GetDatauserListAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 根据用户编号查询表
        /// </summary>
        /// <param name="groupNum"></param>
        /// <returns></returns>

        public sysDataSet.userListDataTable  getuserListAll(string userNum)
        {
            try
            {

                return userListAdapter .GetDatauserListByNum(userNum);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }




        /// <summary>
        /// 插入信息
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int userListInsert(userListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();

                userListDAL userList = new userListDAL(tn, conn);
                userList.userListInsert(row);

                tn.Commit();
                return i;
            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个用户编号已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>

        public int userListUpdate(userListModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                userListDAL userList = new userListDAL(tn, conn);
                userList.userListUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="itemNum"></param>
        /// <returns></returns>

        public int userListDel(string userNum)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                userListDAL userList = new userListDAL(tn, conn);
                userList.userListDel(userNum);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
